<?php

class ProductPhotoTable extends Doctrine_Table {

    const TYPE_NORMAL = 0;
    const TYPE_MAIN = 1;
    const ACTIVE = 1;
    const PASSIVE = 0;

    public static function getInstance() {
        return Doctrine_Core::getTable('ProductPhoto');
    }

    public static function retrieveByPK($id) {
        $q = self::getInstance()->createQuery();
        if (is_array($id)) {
            $q->whereIn('id', $id);
            $q->addWhere('is_active = ?', 1);
            return $q->execute();
        } else {
            $q->where("id = ?", $id);
            return $q->fetchOne();
        }
    }

    public static function retrieveByName($name) {
        $q = self::getInstance()->createQuery()
                ->where("image_name = ?", $name);

        return $q->fetchOne();
    }

    public static function getList($productId, $asArray = false) {
        $query = self::getInstance()->createQuery();
        if (!$productId) {
            return;
        }

        if ($productId) {
            $query->where('product_id = ?', $productId);
        }

        if ($asArray) {
            return $query->fetchArray();
        }

        return $query->execute();
    }

    public static function unsetCover($productId) {
        $query = self::getInstance()->createQuery();

        if (!$productId) {
            return;
        }

        if ($productId) {
            $query->where('product_id = ?', $productId);
        }

        $objs = $query->execute();
        foreach ($objs AS $obj) {
            $obj->setType(self::TYPE_NORMAL);
            $obj->save();
        }
    }

}
